import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
import { useKeepAliveStore } from '../store/index'


// 创建一个路由器对象
const router = createRouter({
  history: createWebHashHistory(),
  // 配置具体的路由信息
  routes: [
    {
      path: '/',
      redirect: '/dashboard'
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录页',
        isPage: true,
        keepAlive: false
      },
      component: () => import('../views/login/index.vue'),
    },
    {
      path: '/',
      name: 'home',
      meta: {
        title: '首页',
        isPage: true,
        keepAlive: true
      },
      component: () => import('../views/home/index.vue'),
      children: [
        {
          path: '/dashboard',
          name: 'dashboard',
          meta: {
            title: '控制台',
            isPage: true,
            keepAlive: true
          },
          component: () => import('../views/dashboard/index.vue')
        },
        {
          path: '/short-url',
          name: 'short-url',
          meta: {
            title: '短链接',
            isPage: false,
            keepAlive: false
          },
          children: [
            {
              path: '/url-record',
              name: 'url-record',
              meta: {
                title: '短链接列表',
                isPage: true,
                keepAlive: true
              },
              component: () => import('../views/url-record/index.vue')
            },
            {
              path: '/visit-record',
              name: 'visit-record',
              meta: {
                title: '访问记录',
                isPage: true,
                keepAlive: true
              },
              component: () => import('../views/visit-record/index.vue')
            },
          ]
        },
        {
          path: '/system',
          name: 'system',
          meta: {
            title: '系统配置',
            isPage: false,
            keepAlive: false
          },
          children: [
            {
              path: '/domain-blacklist',
              name: 'domain-blacklist',
              meta: {
                title: '域名黑名单',
                isPage: true,
                keepAlive: true
              },
              component: () => import('../views/domain-blacklist/index.vue')
            },
            {
              path: '/system-user',
              name: 'system-user',
              meta: {
                title: '用户管理',
                isPage: true,
                keepAlive: true
              },
              component: () => import('../views/system-user/index.vue')
            },
          ]
        },
      ]
    },
    {
      path: '/403',
      name: 'Error403',
      meta: {
        title: '403',
        isPage: true,
        keepAlive: false
      },
      component: () => import('../components/Error403.vue')
    },
    {
      // 注意：不可以写通配符*
      // path:'*',
      path: '/:pathMatch(.*)*',
      name: 'Error404',
      meta: {
        title: '404',
        isPage: true,
        keepAlive: false
      },
      component: () => import('../components/Error404.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.isPage) {
    if (to.meta.keepAlive) {
      const { addCachePage } = useKeepAliveStore()
      addCachePage(to.name, to.meta.title)
    }
    next()
  } else {
    router.push('/403')
  }
})

export default router